require('../css/mine.css');

//导入axios
const axios = require('../utils/http.js')
//导入local
const local = require('../utils/local.js');


window.onload = function(){
    utils.footer('mine');

    let avatar = document.querySelector('.avatar img');
    let mine_name = document.querySelector('.mine_name');
    let signature = document.querySelector('.signature');
    let num999 = document.querySelector('.num999');
    let num888 = document.querySelector('.num888');
    let uploadBtn = document.querySelector('#uplode');
    let quit_box = document.querySelector('.quit_box');
    let uplode = document.querySelector('#uplode');
    let avatar_box = document.querySelector('.avatar_box');

    let modalDom = document.querySelector('.modal');
    let continueDom = document.querySelector('.continue');
    let endDom = document.querySelector('.end');

    let sport_data =  document.querySelector('.sport_data');

    let badge_box = document.querySelector('.badge_box');
    // let sport_data =  document.querySelector('.sport_data');


    function renden(){
        //进页面请求axios  刷新数据
        axios.get('/api/user/info').then(res=>{
            console.log(res.data.data);
            
            let imgUrl = res.data.data.imgUrl;
            imgUrl = 'http://47.96.154.185:3701'+imgUrl;
            avatar.src = imgUrl;
    
            let nickName = res.data.data.nickName;
            if(nickName){
                mine_name.innerText = nickName;
            }
    
            let sign = res.data.data.sign;
            if(sign){
                signature.innerText = sign;
            }
    
            let duration = res.data.data.duration;
            if(duration){
                num999.innerText = duration;
            }
    
            let calorie = res.data.data.calorie;
            if(calorie){
                num888.innerText = calorie;
            }
        })
    }
    renden();

    //监听input的change
    //通过axios将file对象传递给后端
    uploadBtn.addEventListener('change',function(){
        //form表单发送
        let file = uploadBtn.files[0];
        let fd = new FormData();
        fd.append('file',file);
        axios.post('/api/shared/uploadPortrait1',fd).then(res=>{
            if(res.data.errno == 0){
                utils.toast(0,'修改头像成功');
                let imgUrl = 'http://47.96.154.185:3701'+res.data.data.url;
                console.log(res.data.data.url);
                //上传成功之后回显
                avatar.src = imgUrl;

                let relImgUrl = res.data.data.url;
                let  data = {
                    //上传数据到后端  图片
                    imgUrl:relImgUrl,
                }
                axios.post('/api/user/changeInfo',data).then(res=>{
                    console.log(res);
                    //调用一次刷新页面
                    renden();
                })
            }
        })
    })

     //点击退出登录按钮回到登录页
     quit_box.addEventListener('click',function(){
         //显示模态框
        modalDom.style.display = 'block';
        //点击取消
        continueDom.addEventListener('click',function(){
            modalDom.style.display = 'none';
        })
        //点击确认退出
        endDom.addEventListener('click',()=>{
            local.clear();
            location.href='./login.html';
        })
        
    })

    avatar.addEventListener('click',function(){
        uplode.click();
        //阻止事件冒泡
        event.stopPropagation();
        //如果用a标签跳转   要阻止浏览器默认行为
        // event.preventDefault();
    })

    //点击跳转到信息修改
    avatar_box.addEventListener('click',function(){
        //跳转到信息修改
        location.href = './info.html';
    })

    //运动数据页跳转
    sport_data.addEventListener('click',function(){
        location.href='./echarts.html';
    })

    //徽章页面跳转
    badge_box.addEventListener('click',function(){
        location.href = './badge.html';
    })







}